/*
 * @Author: your name
 * @Date: 1970-01-01 08:00:00
 * @LastEditTime : 2019-12-25 20:08:18
 * @LastEditors  : Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /linux/home/dy/linux/linux/WorkSpace/c/linux-c/learn/process/process2.c
 */
#include <stdio.h>
#include <unistd.h> 

int main()
{
    pid_t pid = fork(); //创建进程

    if (pid == -1)
    {
        perror("fork error");
        return -1;
    }
    else if (pid == 0) //child
    {
        pid_t pid2 = fork();
        if (pid2 == 0)
        {
            printf("i am a child:my pid is %d,my father is %d\n", getpid(), getppid());
        }
        else
        {
            printf("i am a father:my pid is %d,my father is %d, my child is %d \n", getpid(), getppid(),pid2);
        }
    }
    else //father
    {
        printf("i am a grand father:my pid is %d,my child is %d \n", getpid(),pid); 
    }

    return 0;
}